//
//  VisitorView.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/4.
//  Copyright © 2015年 002. All rights reserved.
//

#import "VisitorView.h"
#import "UIImageView+Extention.h"
#import "UIButton+Extention.h"
#import "Masonry.h"
#import "UILabel+Extention.h"

@interface VisitorView ()

/**
 *  图标图片
 */
@property(nonatomic,strong) UIImageView *iconView;
/**
 *  遮罩图片
 */
@property(nonatomic,strong) UIImageView *maskIconView;
/**
 *  小房子
 */
@property(nonatomic,strong) UIImageView *homeIconView;
/**
 *  描述文字
 */
@property(nonatomic,strong) UILabel *messageLabel;
/**
 *  注册按钮
 */
@property(nonatomic,strong) UIButton *registerButton;
/**
 *  登录按钮
 */
@property(nonatomic,strong) UIButton *loginButton;

@end

@implementation VisitorView

#pragma mark - 构造方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

/**
 *  设置访客视图
 *
 *  @param imageName 图片名称 首页传入 nil
 *  @param message   描述文字
 */
- (void)setupInfoWithImageName:(NSString *)imageName message:(NSString *)message {
    
    self.messageLabel.text = message;
    
    if (imageName == nil) {
        [self startAnimation];
        return;
    }
    
    // 非首页隐藏小房子并且将遮罩视图隐藏在底层
    self.iconView.image = [UIImage imageNamed:imageName];
    self.homeIconView.hidden = YES;
    [self sendSubviewToBack:self.maskIconView];
}


/**
 *  动画开始
 */
- (void)startAnimation {
    
    CABasicAnimation *animation = [[CABasicAnimation alloc] init];
    animation.keyPath = @"transform.rotation";
    
    animation.duration = 20;
    animation.repeatCount = MAXFLOAT;
    animation.toValue = @(2 * M_PI);
    
    // 该动画会绑定在动画图层上，循环执行 当控件被销毁时，动画会一起被销毁
    animation.removedOnCompletion = NO;
    
    [self.iconView.layer addAnimation:animation forKey:nil];
}

#pragma mark - 设置界面
- (void)setupUI {
    
    self.backgroundColor = [[UIColor alloc] initWithWhite:237.0 / 255.0 alpha:1.0];
    
    // 1. 添加控件
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeIconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    // 2. 布局
    // 图标
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    // 房子
    [self.homeIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    // 描述文字
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.iconView.mas_bottom).offset(16);
        make.width.equalTo(@224);
        make.height.equalTo(@36);
    }];
    
    // 注册按钮
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.messageLabel.mas_left);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(16);
        make.width.equalTo(@100);
        make.height.equalTo(@36);
    }];
    
    // 登录按钮
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.messageLabel.mas_right);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(16);
        make.width.equalTo(@100);
        make.height.equalTo(@36);
    }];
    
    // 遮罩图片
    [self.maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.registerButton.mas_bottom);
    }];
    
    // 3. 添加监听方法
    [self.registerButton addTarget:self action:@selector(register) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 监听方法
/**
 *  注册
 */
- (void)register {

//    if([self.delegate respondsToSelector:@selector(visitorLoginViewDidRegister)]) {
//        [self.delegate visitorLoginViewDidRegister];
//    }
//    if(self.vi)
    if (self.registerBlock) {
        self.registerBlock();
    }
}

/**
 *   登录
 */
- (void)login {
//    if ([self.delegate respondsToSelector:@selector(visitorLoginViewDidLogin)]) {
//        [self.delegate visitorLoginViewDidLogin];
//    }
    if (self.loginBlock) {
        self.loginBlock();
    }
}

#pragma mark - 懒加载控件

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [UIImageView initWithImageName:@"visitordiscover_feed_image_smallicon"];
    }
    return _iconView;
}

- (UIImageView *)homeIconView {
    if (!_homeIconView) {
        _homeIconView = [UIImageView initWithImageName:@"visitordiscover_feed_image_house"];
    }
    return _homeIconView;
}

- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [UILabel initWithTitle:@"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜" fontSize:14 color:[UIColor darkGrayColor]];
    }
    return _messageLabel;
}

- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [UIButton initWithTitle:@"注册" color:[UIColor orangeColor] fontSize:14 backImageName:@"common_button_white_disable"];
    }
    return _registerButton;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton initWithTitle:@"登录" color:[UIColor orangeColor] fontSize:14 backImageName:@"common_button_white_disable"];
    }
    return _loginButton;
}

- (UIImageView *)maskIconView {
    if(!_maskIconView) {
        _maskIconView = [UIImageView initWithImageName:@"visitordiscover_feed_mask_smallicon"];
    }
    return _maskIconView;
}

@end
